package com.tanhua.dubbo.api.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tanhua.dubbo.api.QuestionUserLockApi;
import com.tanhua.dubbo.mapper.QuestionUserLockMapper;
import com.tanhua.dubbo.pojo.QuestionUserLock;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService(version = "1.0.0")
public class QuestionUserLockApiImpl implements QuestionUserLockApi {
    @Autowired
    private QuestionUserLockMapper questionUserLockMapper;

    //根据用户id，查询问卷id和锁状态
    @Override
    public List<QuestionUserLock> selectList(Long userId) {
        QueryWrapper<QuestionUserLock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return this.questionUserLockMapper.selectList(queryWrapper);
    }

    @Override
    public QuestionUserLock queryOneByQuestionnaireId(Long questionnaireId) {
        QueryWrapper<QuestionUserLock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("questionnaire_id", questionnaireId);
        return this.questionUserLockMapper.selectOne(queryWrapper);
    }

    @Override
    public void updateByQuestionnaireId(QuestionUserLock questionUserLock, Long questionnaireId) {
        QueryWrapper<QuestionUserLock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("questionnaire_id", questionnaireId);
        this.questionUserLockMapper.update(questionUserLock, queryWrapper);
    }

    @Override
    public void updateByUserId(Long userId, QuestionUserLock questionUserLock, Long questionnaireId) {
        QueryWrapper<QuestionUserLock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                .eq("questionnaire_id", questionnaireId + 1);
        this.questionUserLockMapper.update(questionUserLock, queryWrapper);

    }

    @Override
    public void insert(QuestionUserLock questionUserLock) {
        this.questionUserLockMapper.insert(questionUserLock);
    }
}
